﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class git_diff : CommonPage
    {
        private string left_out = "";
        private string right_out = "";
        private string unified_diff_text = "";
        private string left_title = "";
        private string right_title = "";
        private string path = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);


            // get info about revision

            string sql = @"
select gitcom_commit, gitcom_bug, gitcom_repository, gitap_path 
from git_commits
inner join git_affected_paths on gitap_gitcom_id = gitcom_id
where gitap_id = $id";

            int gitap_id = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));
            sql = sql.Replace("$id", Convert.ToString(gitap_id));

            DataRow dr = DbUtil.get_datarow(sql);

            // check if user has permission for this bug
            int permission_level = Bug.get_bug_permission_level((int)dr["gitcom_bug"], security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }


            string repo = (string)dr["gitcom_repository"];
            path = (string)dr["gitap_path"];

            string error = "";

            string commit0 = Request["rev_0"];

            if (string.IsNullOrEmpty(commit0))
            {
                string commit = (string)dr["gitcom_commit"];

                unified_diff_text = VersionControl.git_get_unified_diff_one_commit(repo, commit, path);

                // get the source code for both the left and right
                string left_text = VersionControl.git_get_file_contents(repo, commit + "^", path);
                string right_text = VersionControl.git_get_file_contents(repo, commit, path);
                left_title = commit + "^";
                right_title = commit;

                error = VersionControl.visual_diff(unified_diff_text, left_text, right_text, ref left_out, ref right_out);

            }
            else
            {

                string commit1 = Request["rev_1"];

                unified_diff_text = VersionControl.git_get_unified_diff_two_commits(repo, commit0, commit1, path);

                // get the source code for both the left and right
                string left_text = VersionControl.git_get_file_contents(repo, commit0, path);
                string right_text = VersionControl.git_get_file_contents(repo, commit1, path);
                left_title = commit0;
                right_title = commit1;

                error = VersionControl.visual_diff(unified_diff_text, left_text, right_text, ref left_out, ref right_out);

            }

            if (error != "")
            {
                Response.Write(error);
                Response.End();
            }
        }
    }
}
